
package com.xiaoma.myaudience.biz.view;

import java.util.Map;

import android.content.Context;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Transformation;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.FrameLayout;
import android.widget.Gallery;
import android.widget.TextView;

import com.xiaoma.myaudience.R;
import com.xiaoma.myaudience.biz.adapter.HeadlineAdapter;
import com.xiaoma.myaudience.biz.model.HeadlineVideoInfo;
import com.xiaoma.myaudience.biz.model.HomeInfoModel;
import com.xiaoma.myaudience.util.model.ModelUtils;

/**
 * 首页头条推荐组件.
 * 
 */
public class HeadlineGallery extends FrameLayout implements OnItemSelectedListener {
    private static final String TAG = "HeadlineGallery";

    /**
     * Gallery
     */
    private MyGallery mGallery;

    /**
     * 指示器
     */
    private Indicator mIndicator;
    
    private TextView mTitle;

    private HeadlineAdapter mAdapter;

    public HeadlineGallery(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public HeadlineGallery(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public HeadlineGallery(Context context) {
        super(context);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        mGallery = new MyGallery(getContext());
        mGallery.setOnItemSelectedListener(this);
        FrameLayout.LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT,
                (int) getResources().getDimension(R.dimen.headline_img_height));
        addView(mGallery, 0, lp);

        mIndicator = (Indicator) findViewById(R.id.headline_indicator);
        mTitle = (TextView) findViewById(R.id.headline_title);
    }

    public void setAdapter(HeadlineAdapter adapter) {
        mAdapter = adapter;
        mGallery.setAdapter(adapter);
        if (adapter != null) {
            mGallery.setSelection(mAdapter.getInitPosition(0));
        }
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        mGallery.setOnItemClickListener(listener);
    }

    @Override
    public void onItemSelected(AdapterView<?> adapter, View arg1, int position, long arg3) {
        final int count = mAdapter.getRealCount();
        if (mIndicator.getTotalItems() != count) {
            mIndicator.setTotalItems(count);
        }

        int realPosition = mAdapter.getRealPosition(position);
        Map<String, Object> map = (Map<String, Object>) adapter.getItemAtPosition(realPosition);
//        HeadlineVideoInfo info = (HeadlineVideoInfo)adapter.getItemAtPosition(realPosition);
        mTitle.setText(ModelUtils.getStringValue(map, HomeInfoModel.PARAM_TITLE));
        
        mIndicator.setCurrentItem(realPosition);
    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
    }
    
    public void startAutoScroll(boolean autoScroll) {
        if (mGallery != null) {
            mGallery.startAutoScroll(autoScroll);
        }
    } 

    /**
     * fling 只滑动一屏的Gallery.<br/>
     * 
     * @author wjying
     */
    private class MyGallery extends Gallery {
        private static final int AUTO_CHANGE_TIME = 5000;

        /**
         * 自动滑动切换Runnable.
         */
        private Runnable mAutoRunnable = new Runnable() {
            @Override
            public void run() {
                scrollRight();
                postDelayed(this, AUTO_CHANGE_TIME);
            }
        };

        public MyGallery(Context context) {
            super(context);
//            setSpacing(0);
            setAnimationDuration(1000);
            setSoundEffectsEnabled(false);
            setFadingEdgeLength(0);
        }
        
        @Override
        protected boolean getChildStaticTransformation(View child, Transformation t) {
            return false;
        }

        @Override
        public boolean onTouchEvent(MotionEvent event) {
            startAutoScroll(true);
            return super.onTouchEvent(event);
        }

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            // return super.onFling(e1, e2, velocityX, velocityY);
            if (e2.getX() > e1.getX()) {
                scrollLeft();
            } else {
                scrollRight();
            }
            return true;
        }

        private void scrollLeft() {
            onKeyDown(KeyEvent.KEYCODE_DPAD_LEFT, null);
        }

        private void scrollRight() {
            onKeyDown(KeyEvent.KEYCODE_DPAD_RIGHT, null);
        }

        @Override
        public void onWindowFocusChanged(boolean hasWindowFocus) {
            super.onWindowFocusChanged(hasWindowFocus);
            if (hasWindowFocus) {
                startAutoScroll(true);
            } else {
                startAutoScroll(false);
                HeadlineAdapter adapter = (HeadlineAdapter) getAdapter();
                if (adapter != null) {
                    setSelection(adapter.getInitPosition(getSelectedItemPosition()));
                }
            }
        }
        
        public void startAutoScroll(boolean autoScroll) {
            if (autoScroll) {
                removeCallbacks(mAutoRunnable);
                postDelayed(mAutoRunnable, AUTO_CHANGE_TIME);
            } else {
                removeCallbacks(mAutoRunnable);
            }
        }

    }

}
